﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Text;

namespace Weixin.Utils
{
    /// <summary>
    ///WebRequestHelper 的摘要说明
    /// </summary>
    public class WebRequestHelper
    {
        public WebRequestHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// Post数据
        /// </summary>
        /// <param name="posturl">Url地址</param>
        /// <param name="postData">post数据字符串</param>
        /// <returns>执行结果</returns>
        public static string PostData(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(postData);
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页（html）代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                Logger.Log4Net.Error(ex.Message + "\r\n" + ex.StackTrace);
                return string.Empty;
            }
        }

        public static string GetData(string url, string data)
        {
            string result = string.Empty;
            
            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + "?" + data);
                req.Method = "GET";
                //WebResponse wr = req.GetResponse();
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                //StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                result = "";
                Logger.Log4Net.Error(ex.Message + "\r\n" + ex.StackTrace);
            }

            return result;
        }
    }
}